<?php

//error_reporting('E_NONE'); //Setting this to E_ALL showed that that cause of not redirecting were few blank lines added in some php files.
$install_success = 0;
$db_config_path = '../application/config/database.php';
$ci_config_path = '../application/config/config.php';
$auth_config_path = '../application/config/ion_auth.php';

  $baseurl = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
  $baseurl .= "://".$_SERVER['HTTP_HOST'];
  $baseurl .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
  $baseurl = str_replace('install/','',$baseurl); 
			

// Only load the classes in case the user submitted the form
if($_POST) {

	// Load the classes and create the new objects
	require_once('includes/core_class.php');
	require_once('includes/database_class.php');

	$core = new Core();
	$database = new Database();


	// Validate the post data
	if($core->validate_post($_POST) == true)
	{

		// First create the database, then create tables, then write config file
		if($database->create_database($_POST) == false) {
			$message = $core->show_message('error',"The database could not be created, please verify your settings.");
		} else if ($database->create_tables($_POST) == false) {
			$message = $core->show_message('error',"The database tables could not be created, please verify your settings.");
		} else if ($core->write_config($_POST) == false) {
			$message = $core->show_message('error',"The database configuration file could not be written, please chmod application/config/database.php file to 777");
		}

		// If no errors, redirect to registration page

		 if(!isset($message)) {
		 	$install_success = 1;
		}

	}
	else {
		$message = $core->show_message('error','Not all fields have been filled in correctly. The host, username, password, and database name are required.');
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<title>Installatie | Tourpoule 2012</title>

    <link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="../assets/bootstrap/css/bootstrap.css">
    
    <link rel="stylesheet" href="../assets/css/style.css">

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
    <script type="text/javascript" src="../assets/bootstrap/js/bootstrap.js"></script>
    <link type="text/css" href="../assets/css/custom-theme/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
	  <script>
	  $(document).ready(function(){


	    $("#install_form").validate();
	  });
	  </script>
	  <style>
	  label.error { color:red; }
	  </style>
	</head>
	<body>
		<div class='container'>
			<div class='row'>
				<div class='span12'>
			  <?php if (!$install_success) { ?>
			    <?php if( (is_writable($db_config_path) OR !file_exists($db_config_path))
			    					AND
			    					(is_writable($ci_config_path) OR !file_exists($ci_config_path))
			    					AND
			    					(is_writable($auth_config_path) OR !file_exists($auth_config_path))
			    				){?>

					  <?php if(isset($message)) {echo '<div class="alert alert-error">' . $message . '</div>';}?>
					  <div class='span6'>
					  	<div class='panel'>
					  		<h1>Installatie Tourpoule</h1>
							  <form id="install_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-horizontal">
					        <fieldset>
					          <legend>Database settings</legend>
					          <div class='control-group'>
					          	<label class='control-label' for="hostname">Databse host</label>
					          	<div class='controls'>
					          		<input type="text" id="hostname" value="localhost" class="input_text required" name="hostname" />
					          	</div>
					          </div>
					          <div class='control-group'>
						          <label class='control-label' for="username">Gebruiker</label>
						          <div class='controls'>
						          	<input type="text" id="username" class="input_text required" name="username" />
						          </div>
					          </div>
					          <div class='control-group'>
					          	<label class='control-label' for="password">Wachtwoord</label>
					          	<div class='controls'>
					          		<input type="password" id="password" class="input_text" name="password" />
					          	</div>	
					          </div>
					          <div class='control-group'>
					          	<label class='control-label' for="database">Database Name</label>
					          	<div class='controls'>
					          		<input type="text" id="database" class="input_text required" name="database" />
					          		<span class="help-block">Als de database niet bestaat, zal worden geprobeerd om deze aan te maken. Niet alle servers geven hier toestemming voor.</span>
					          	</div>
					          </div>
					          <div class='control-group'>
					          	<label class='control-label' for="database_prefix">Database Prefix</label>
					          	<div class='controls'>
					          		<input type="text" id="database_prefix" value="tp12_" class="input_text" name="database_prefix" />
					          		<span class="help-block">Deze prefix wordt teogevoegd aan alle tabellen, zodat je de installtie aan een bestaande database kunt toevoegen.</span>
					          	</div>
					          </div>
					          
					        </fieldset>
							  <fieldset>
					          <legend>Pool settings</legend>
					          <div class='control-group'>
						          <label class='control-label' for="hostname">Site Naam</label>
						          <div class='controls'>
						          	<input type="text" id="sitename" value="Tourpoule 2012" class="input_text required" name="sitename" />
						          	<span class="help-block">De titel of naam van je website.</span>
						          </div>
						        </div>	
					          <div class='control-group'>
					          	<label class='control-label' for="username">Beheer e-mail</label>
					          	<div class='controls'>
					          		<input type="text" id="adminemail" class="input_text required email" name="adminemail" />
					          		<span class="help-block">De afzender van e-mails aan gebruikers. Dit e-mail adres wordt ok gebruikt om als beheerder in te loggen.</span>
						          </div>
						        </div>
						        <div class='control-group'>
					          	<label class='control-label' for="adminpassword">Beheerder Wachtwoord</label>
					          	<div class='controls'>
					          		<input type="password" id="adminpassword" class="input_text" name="adminpassword" minlenghth="8" maxlength="20" />
					          		<span class="help-block">Wachtwoord voor het beheerdersaccount.</span>
					          	</div>	
					          </div>
						        <div class='control-group'>
					          	<label class='control-label' for="baseurl">Site URL</label>
					          	<div class='controls'>
					          		<input type="text" id="baseurl" value="<?php echo $baseurl;?>" class="input_text required" name="baseurl" />
					          		<span class="help-block">De URL van jouw website, inclusief "http://" én een slash "/" aan het eind.</span>
						          </div>
						        </div>					        
					        </fieldset>
									<input type="submit" value="Installeren" id="submit" class='btn btn-primary' />
							  </form>

						  <?php } else { ?>
					      <div class="alert alert-error">Please make the files application/config/database.php, application/config/config.php and application/config.ion_auth.php writable. <strong>Example</strong>:<br /><br /><code>chmod 777 application/config/database.php</code></div>
						  <?php } ?>
						</div>
					</div>
					<?php } else {
						?>
						<div class='alert alert-success'>Installatie succesvol! <a href="<?php echo $baseurl; ?>">Ga naar de Tourpoule</a>.</div> 
						<?php
					} ?>
				</div>
			</div>
		</div>

	</body>
</html>